package com.pkdl.dao;

import java.sql.CallableStatement;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.logging.Level;
import java.util.logging.Logger;

import com.pkdl.dto.BenhNhanChoKhamDTO;

// Data access object, which get data of BenhNhanChoKham table from database
public class BenhNhanChoKhamDAO {
	public boolean delete(int mabenhnhan) {
		boolean result = true;
		Connection connect = null;
		try {
			connect = DataAccessHelper.getConnection();
			CallableStatement cs = connect
					.prepareCall("{call usp_BenhNhanChoKham_Delete(?)}");
			cs.setInt(1, mabenhnhan);
			int n = cs.executeUpdate();
			if (n == 0) {
				result = false;
			}
			connect.close();
		} catch (SQLException ex) {
			Logger.getLogger(BenhNhanChoKhamDAO.class.getName()).log(
					Level.SEVERE, null, ex);
		} finally {
			try {
				connect.close();
			} catch (SQLException ex) {
				Logger.getLogger(BenhNhanChoKhamDAO.class.getName()).log(
						Level.SEVERE, null, ex);
			}
		}
		return result;
	}

	public boolean insert(BenhNhanChoKhamDTO benhnhanchokham) {
		boolean result = true;
		Connection connect = null;
		try {
			connect = DataAccessHelper.getConnection();
			CallableStatement cs = connect
					.prepareCall("{call usp_BenhNhanChoKham_Insert(?, ?, ?)}");
			cs.setInt(1, benhnhanchokham.getMaBenhNhan());
			cs.setTimestamp(2, new java.sql.Timestamp(benhnhanchokham
					.getThoiDiemTiepNhan().getTime()));
			cs.setString(3, benhnhanchokham.getTinhTrang());
			int n = cs.executeUpdate();
			if (n == 0) {
				result = false;
			}
			connect.close();
		} catch (SQLException ex) {
			Logger.getLogger(BenhNhanChoKhamDAO.class.getName()).log(
					Level.SEVERE, null, ex);
		} finally {
			try {
				connect.close();
			} catch (SQLException ex) {
				Logger.getLogger(BenhNhanChoKhamDAO.class.getName()).log(
						Level.SEVERE, null, ex);
			}
		}
		return result;
	}

	public ArrayList<BenhNhanChoKhamDTO> selectAll() {
		ArrayList<BenhNhanChoKhamDTO> list = new ArrayList<BenhNhanChoKhamDTO>();
		Connection connect = null;
		try {
			connect = DataAccessHelper.getConnection();
			CallableStatement cs = connect
					.prepareCall("{call usp_BenhNhanChoKham_SelectAll()}");
			ResultSet rs = cs.executeQuery();
			while (rs.next()) {
				BenhNhanChoKhamDTO benhnhanchokham = new BenhNhanChoKhamDTO();
				benhnhanchokham.setMaBenhNhan(rs.getInt("MaBenhNhan"));
				benhnhanchokham.setThoiDiemTiepNhan(rs
						.getTimestamp("ThoiDiemTiepNhan"));
				benhnhanchokham.setTinhTrang(rs.getString("TinhTrang"));
				list.add(benhnhanchokham);
			}
			connect.close();
		} catch (SQLException ex) {
			Logger.getLogger(BenhNhanChoKhamDAO.class.getName()).log(
					Level.SEVERE, null, ex);
		} finally {
			try {
				connect.close();
			} catch (SQLException ex) {
				Logger.getLogger(BenhNhanChoKhamDAO.class.getName()).log(
						Level.SEVERE, null, ex);
			}
		}
		return list;
	}

	public BenhNhanChoKhamDTO selectById(int mabenhnhan) {
		BenhNhanChoKhamDTO benhnhanchokham = new BenhNhanChoKhamDTO();
		Connection connect = null;
		try {
			connect = DataAccessHelper.getConnection();
			CallableStatement cs = connect
					.prepareCall("{call usp_BenhNhanChoKham_SelectByID(?)}");
			cs.setInt(1, mabenhnhan);
			ResultSet rs = cs.executeQuery();
			while (rs.next()) {
				benhnhanchokham.setMaBenhNhan(rs.getInt("MaBenhNhan"));
				benhnhanchokham.setThoiDiemTiepNhan(rs
						.getTimestamp("ThoiDiemTiepNhan"));
				benhnhanchokham.setTinhTrang(rs.getString("TinhTrang"));
			}
			connect.close();
		} catch (SQLException ex) {
			Logger.getLogger(BenhNhanChoKhamDAO.class.getName()).log(
					Level.SEVERE, null, ex);
		} finally {
			try {
				connect.close();
			} catch (SQLException ex) {
				Logger.getLogger(BenhNhanChoKhamDAO.class.getName()).log(
						Level.SEVERE, null, ex);
			}
		}
		return benhnhanchokham;
	}

	public ArrayList<BenhNhanChoKhamDTO> selectByMaBenhNhan(int mabenhnhan) {
		ArrayList<BenhNhanChoKhamDTO> list = new ArrayList<BenhNhanChoKhamDTO>();
		Connection connect = null;
		try {
			connect = DataAccessHelper.getConnection();
			CallableStatement cs = connect
					.prepareCall("{call usp_BenhNhanChoKham_SelectByMaBenhNhan(?)}");
			cs.setInt(1, mabenhnhan);
			ResultSet rs = cs.executeQuery();
			while (rs.next()) {
				BenhNhanChoKhamDTO benhnhanchokham = new BenhNhanChoKhamDTO();
				benhnhanchokham.setMaBenhNhan(rs.getInt("MaBenhNhan"));
				benhnhanchokham.setThoiDiemTiepNhan(rs
						.getTimestamp("ThoiDiemTiepNhan"));
				benhnhanchokham.setTinhTrang(rs.getString("TinhTrang"));
				list.add(benhnhanchokham);
			}
			connect.close();
		} catch (SQLException ex) {
			Logger.getLogger(BenhNhanChoKhamDAO.class.getName()).log(
					Level.SEVERE, null, ex);
		} finally {
			try {
				connect.close();
			} catch (SQLException ex) {
				Logger.getLogger(BenhNhanChoKhamDAO.class.getName()).log(
						Level.SEVERE, null, ex);
			}
		}
		return list;
	}

	public boolean update(BenhNhanChoKhamDTO benhnhanchokham) {
		boolean result = true;
		Connection connect = null;
		try {
			connect = DataAccessHelper.getConnection();
			CallableStatement cs = connect
					.prepareCall("{call usp_BenhNhanChoKham_Update(?, ?)}");
			cs.setInt(1, benhnhanchokham.getMaBenhNhan());
			cs.setString(2, benhnhanchokham.getTinhTrang());
			int n = cs.executeUpdate();
			if (n == 0) {
				result = false;
			}
			connect.close();
		} catch (SQLException ex) {
			Logger.getLogger(BenhNhanChoKhamDAO.class.getName()).log(
					Level.SEVERE, null, ex);
		} finally {
			try {
				connect.close();
			} catch (SQLException ex) {
				Logger.getLogger(BenhNhanChoKhamDAO.class.getName()).log(
						Level.SEVERE, null, ex);
			}
		}
		return result;
	}

}
